package org.msss.gamebasic;

import org.lwjgl.util.Rectangle;
import org.newdawn.slick.opengl.Texture;
import java.util.ArrayList;

/**
 * Things like a tile or background would extend Icon.
 * 
 * @author alanxoc3
 * 
 */
public class Icon {

	// Set Defaults.
	public static final int DEFAULT_X = 0;
	public static final int DEFAULT_Y = 0;
	public static final int DEFAULT_IMAGE_W = 32;
	public static final int DEFAULT_IMAGE_H = 32;
	public static final int DEFAULT_Z = 0;
	public static final int DEFAULT_ANIMATION_SPEED = 15;

	// Some Variables.
	private int z;
	private Rectangle imageBounds;
	private ArrayList<Texture> imageStrip;
	private boolean animationOn;
	private int currentFrame, animationSpeed;

	public Icon(Texture image) {
		imageStrip = new ArrayList<Texture>();
		animationOn = false;
		currentFrame = 0;
		animationSpeed = DEFAULT_ANIMATION_SPEED;

		imageBounds = new Rectangle(DEFAULT_X, DEFAULT_Y, DEFAULT_IMAGE_W,
				DEFAULT_IMAGE_H);

		z = DEFAULT_Z;

		if (image != null) {
			imageStrip.add(image);
			setImageW(image.getImageWidth());
			setImageH(image.getImageHeight());
		}
	}

	/**
	 * Default Constructor.
	 */
	public Icon() {
		this(null);
	}

	/**
	 * This method does the logic for animating the icon.
	 */
	public void timer() {
		if (animationOn && getSize() > 0) {
			currentFrame = (Timer.getStepTime() / animationSpeed) % (getSize());
		}
	}

	// Getters and setters... and adders
	public int getX() {
		return imageBounds.getX();
	}

	public void setX(int x) {
		imageBounds.setX(x);
	}

	public int getY() {
		return imageBounds.getY();
	}

	public void setY(int y) {
		imageBounds.setY(y);
	}

	public int getImageW() {
		return imageBounds.getWidth();
	}

	public void setImageW(int w) {
		imageBounds.setWidth(w);
	}

	public int getImageH() {
		return imageBounds.getHeight();
	}

	public void setImageH(int h) {
		imageBounds.setHeight(h);
	}

	public int getZ() {
		return z;
	}

	public void setZ(int z) {
		this.z = z;
	}

	public Rectangle getImageBounds() {
		return imageBounds;
	}

	public void setImageBounds(Rectangle bounds) {
		this.imageBounds = bounds;
	}

	public int getSize() {
		return imageStrip.size();
	}

	public void addImage(Texture newImage) {
		if (newImage != null) {
			imageStrip.add(newImage);

			setImageW(newImage.getImageWidth());
			setImageH(newImage.getImageHeight());
		}
	}

	public Texture getImage(int index) {
		return imageStrip.get(index);
	}

	public void setImage(int index, Texture newImage) {
		if (newImage != null) {
			imageStrip.set(index, newImage);
		}
	}

	public ArrayList<Texture> getImageStrip() {
		return imageStrip;
	}

	public void setImageStrip(ArrayList<Texture> imageStrip) {
		this.imageStrip = imageStrip;
	}

	public boolean isAnimationOn() {
		return animationOn;
	}

	public void setAnimationOn(boolean animationOn) {
		this.animationOn = animationOn;
	}

	public int getCurrentFrame() {
		return currentFrame;
	}

	public void setCurrentFrame(int currentFrame) {
		this.currentFrame = currentFrame;
	}

	public int getSpeed() {
		return animationSpeed;
	}

	public void setSpeed(int speed) {
		this.animationSpeed = speed;
	}

	public Texture getCurrentImage() {
		if (imageStrip.size() == 0) {
			return null;
		} else {
			return imageStrip.get(currentFrame);
		}
	}

	public int getAnimationSpeed() {
		return animationSpeed;
	}

	public void setAnimationSpeed(int animationSpeed) {
		this.animationSpeed = animationSpeed;
	}

}
